package kit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import model.Info;
import service.Build;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/10/17.
 */
public class Kit {

    public static final String CITYTR = "citytr";
    public static final String COUNTRYTR = "countytr";
    public static final String TOWNtR = "towntr";
    public static final String VALI = "villagetr";

    public static List<Info> build(String type,String baseUrl,String url, Build build,int level)throws Exception{
        StringBuilder log = new StringBuilder();
        for(int i = 0;i<level;i++){
            log.append("  ");
        }
        log.append(type+" url:"+baseUrl+url);
        System.out.println(log.toString());
        String html = Kit.getUrl(baseUrl + url);
        try {
            List<Info> result = Kit.getInfo(html, type);
            if (level >= 5 || build == null) {
                System.out.println("结束：level:"+level+" build="+build);
                return result;
            }
            for (Info item : result) {
                String[] u = item.getUrl().split("/");
                if(u.length != 2){continue;}
                List<Info> child = build.extract(baseUrl + u[0] + "/", u[1], (level + 1));
                item.setList(child);
            }
            return result;
        }catch (Exception e){
            System.err.println(html);
            throw new RuntimeException(type +" error",e);
        }
    }



    private static String getType(String html){
        if(html.indexOf("class='towntr'") > 0){
            return "towntr";
        }else if(html.indexOf("class='villagetr'") > 0){
            return "villagetr";
        }
        return "errortr";
    }

    private static List<Info> getInfo(String html,String type){
        try {
            if(html.indexOf(type) < 0){
                type = getType(html);
            }
            Matcher m = Pattern.compile("<tr class='" + type + "'>([\\s\\S]*)</tr>").matcher(html);
            m.find();
            String tds = m.group(1);
            Matcher mm = Pattern.compile("<td>(.*?)</td>").matcher(tds);
            int i = 1;
            List<Info> result = new ArrayList<Info>();
            while (mm.find()) {
                String td = mm.group(1);
                if(type.equals("villagetr")) {
                    if(i % 3 == 0){
                        String rr = td.replaceAll("<td>", "")
                                .replaceAll("</td>", "");
                            result.add(new Info(rr,""));
                    }

                }else {
                    if (i % 2 == 0) {
                        String rr[] = td.replaceAll("<td>", "")
                                .replaceAll("<a href='", "")
                                .replaceAll("</a>", "")
                                .replaceAll("</td>", "")
                                .split("'>");
                        if(rr.length == 2 && !"省直辖县级行政区划".equals(rr[1]) && !"自治区直辖县级行政区划".equals(rr[1]) && !"市辖区".equals(rr[1])){
                            result.add(new Info(rr[1], rr[0]));
                        }
                    }
                }
                i++;
            }
            return result;
        }catch (Exception e){
            System.err.println(html);
            System.err.println(type);
            e.printStackTrace();
            throw new RuntimeException("mathc error ",e);
        }
    }

    public static String getUrl(String _url)throws  Exception{
        String filePath = "E:/cityFiles/" + URLEncoder.encode(_url,"utf-8");
        File file = new File(filePath);
        if(file.exists()){
            String html = getFile(filePath,"utf-8");
            System.out.println(html);
            return html; // getFile(filePath,"gbk");
        }
        String html = rrequest(_url);
        saveFile(filePath,html);
        return html;
    }

    private static String rrequest(String _url){
        int i = 0 ;int max = 5;
        while(i < 5){
            try{
                return request(_url);
            }catch (Exception e){
                if(e instanceof TimeoutException){
                    System.out.println("time out error.");
                }
            }
            i ++;
        }
        throw new RuntimeException("结束吧，从新来吧。已经救不了了。");
    }

    private static String request(String _url) throws Exception{
        URL url = new URL(_url);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(1000 * 3);
        con.setConnectTimeout(1000 * 3);

        InputStream in = con.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in, "gb2312"));
        StringBuilder result = new StringBuilder();
        String temp;
        while ((temp = read.readLine()) != null) {
            result.append(temp);
        }
        read.close();
        in.close();
        return result.toString();
    }

    private static void saveFile(String path,String html) throws Exception{
        File file = new File(path);
        Writer writer = new FileWriter(file);
        writer.append(html);
        writer.close();
    }


    public static void toFile(String fileName, List<Info> result) throws Exception{
        JSONObject json = new JSONObject();
        json.put("name",fileName);
        JSONArray list = (JSONArray)JSON.toJSON(result);//.parseArray(JSON.toJSONString(result));
        json.put("list",list);
        System.out.println(json);
        File file = new File("e:/citys/"+fileName+".json");
        Writer writer =  new FileWriter(file);
        writer.append(json.toJSONString());
        writer.close();
    }

    public static String getFile(String path,String charset) throws Exception{
        File file = new File(path);
        BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(file),charset));
        String temp;
        StringBuilder result = new StringBuilder();
        while((temp = read.readLine()) != null){
            if(temp.trim().length() == 0){continue;}
            result.append(temp.trim());
        }
        read.close();
        return result.toString();
    }
}
